package core

import (
	"fmt"
	"github.com/shopspring/decimal"
	"github.com/sony/sonyflake"
	"io/ioutil"
	"matcher/pkg/boot/config"
	"matcher/pkg/boot/logger"
	"matcher/pkg/lib/helper"
)

var (
	Matchers   []*Matcher
	Symbol2Idx map[string]int
	CacheFile  = "/runtime/matchers.json"
	SF         *sonyflake.Sonyflake
	Billion    decimal.Decimal
)

func Start() {
	Billion = helper.NewDecimal("1000000000")
	SF = sonyflake.NewSonyflake(sonyflake.Settings{})
	Restore()
}

func Restore() {
	Symbol2Idx = make(map[string]int)
	Matchers = make([]*Matcher, 0)

	for idx, symbol := range config.Symbols {
		var path = fmt.Sprintf("%s/runtime/symbols/%s.json", config.ROOT_PATH, symbol)
		var jsonData = ""
		if helper.PathExists(path) {
			content, _ := ioutil.ReadFile(path)
			jsonData = string(content)
		}
		Symbol2Idx[symbol] = idx
		Matchers = append(Matchers, NewMatcher(symbol, idx, jsonData))
	}
}

func Stop() {
	logger.Info("stopping...")
	for _, item := range Matchers {
		item.Refresh()
	}
}
